{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [best-time-to-buy-and-sell-stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-01T08:09:43.678898Z",
     "start_time": "2020-11-01T08:09:43.493Z"
    }
   },
   "source": [
    "**问题：**\n",
    "```\n",
    "假设您有一个数组，第i个元素是第i天给定股票的价格。\n",
    "如果只允许您最多完成一笔交易（即买入和卖出一股股票），则设计一种算法以找到最大的利润。\n",
    "请注意，您不能在买股票之前卖出股票。\n",
    "```\n",
    "**示例：**\n",
    "```\n",
    "Example 1:\n",
    "Input: [7,1,5,3,6,4]\n",
    "Output: 5\n",
    "说明：在第2天买入（价格= 1）并在第5天卖出（价格= 6），利润= 6-1 = 5。\n",
    "不是7-1 = 6，因为销售价格必须大于购买价格。\n",
    "\n",
    "Example 2:\n",
    "Input: [7,6,4,3,1]\n",
    "Output: 0\n",
    "说明：在这种情况下，不执行任何交易，即最大利润= 0。\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "/**\n",
    " * @param {number[]} prices\n",
    " * @return {number}\n",
    " */\n",
    "var maxProfit = function(prices) {\n",
    "    let min = prices[0];\n",
    "    let profit = 0;\n",
    "    // 7 1 5 3 6 4\n",
    "    for(let i = 1; i < prices.length; i++) {\n",
    "        if (prices[i] > prices[i -1]) {\n",
    "            profit = Math.max(profit, prices[i] - min);\n",
    "        } else {\n",
    "            min = Math.min(min, prices[i]);;\n",
    "        }\n",
    "    }\n",
    "\n",
    "    return profit;\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let arr = [7, 1, 5, 3, 6, 4]\n",
    "maxProfit(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fibonacci"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-01T08:08:23.039849Z",
     "start_time": "2020-11-01T08:08:22.848Z"
    }
   },
   "outputs": [],
   "source": [
    "var fib = (n) => n === 0 ? 0 : n === 1 ? 1 : fib(n - 1) + fib(n - 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-01T08:08:49.927143Z",
     "start_time": "2020-11-01T08:08:49.738Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "console.log(fib(1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Javascript (Node.js)",
   "language": "javascript",
   "name": "javascript"
  },
  "language_info": {
   "file_extension": ".js",
   "mimetype": "application/javascript",
   "name": "javascript",
   "version": "13.13.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
